package com.BCBDexther.magichealth;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Hashtable;

import org.ksoap2.serialization.KvmSerializable;
import org.ksoap2.serialization.PropertyInfo;

public class BCBSecurity implements KvmSerializable
{
	String codeEditeur="ECEPARIS";
	String secretEditeur="";
	String idPS="0";
	
	
	public BCBSecurity() {
		super();
		this.codeEditeur="ECEPARIS";
		this.idPS="0";
		this.secretEditeur = generateKey(codeEditeur, idPS);
	}
	public String getCodeEditeur() {
		return codeEditeur;
	}
	public void setCodeEditeur(String codeEditeur) {
		this.codeEditeur = codeEditeur;
		this.secretEditeur = generateKey(codeEditeur, idPS);
	}
	public String getSecretEditeur() {
		return this.secretEditeur;
	}
	public void setSecretEditeur(String secretEditeur) {
		this.secretEditeur = secretEditeur;
	}
	public String getIdPS() {
		return idPS;
	}
	public void setIdPS(String idPS) {
		this.idPS = idPS;
		this.secretEditeur = generateKey(codeEditeur, idPS);
	}
	
	
	@Override
	public Object getProperty(int index) {
		switch(index)
		{	
		case 0: return codeEditeur;
		case 1: return idPS;
		case 2: return secretEditeur;
		}
		return null;
	}
	
	@Override
	public int getPropertyCount() {
		
		return 3;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public void getPropertyInfo(int index, Hashtable arg1, PropertyInfo info) {
		switch(index)
		{	
		case 0: 
			info.type=PropertyInfo.STRING_CLASS;
			info.name="codeEditeur";
			break;
			
		case 1: 
			info.type=PropertyInfo.STRING_CLASS;
			info.name="idPS";
			break;
			
		case 2: 
			info.type=PropertyInfo.STRING_CLASS;
			info.name="secretEditeur";
			break;
		default:
			break;
		}
		
		
	}
	@Override
	public void setProperty(int index, Object value) {
		switch(index)
		{
		case 0:
			codeEditeur=value.toString();
			break;
			
		case 1:
			idPS=value.toString();
			break;
			
		case 2:
			secretEditeur=value.toString();
			break;
		}
		
	}
	
	public static String generateKey(String codeEditeur,String idPS)
	{
		int tabConversion[]={154, 43, 174, 241, 124, 205, 51, 103, 70, 40, 54, 93, 166, 130, 250, 217, 24, 181, 3, 33, 210, 39, 125, 41, 10, 232, 62, 28, 207, 1, 255, 244, 63, 160, 146, 191, 153, 100, 82, 76, 0, 155, 104, 56, 144, 228, 225, 45, 159, 184, 248, 66, 69, 2, 17, 196, 15, 97, 211, 139, 246, 49, 129, 58, 5, 242, 158, 86, 249, 112, 11, 101, 68, 148, 204, 219, 77, 170, 31, 133, 149, 81, 71, 27, 235, 74, 171, 57, 238, 94, 132, 113, 222, 44, 243, 108, 227, 233, 75, 188, 254, 165, 92, 35, 20, 114, 106, 34, 25, 152, 161, 29, 117, 179, 192, 175, 221, 88, 7, 9, 138, 96, 4, 22, 236, 164, 214, 32, 212, 118, 6, 229, 201, 8, 55, 42, 131, 231, 26, 230, 252, 48, 53, 151, 87, 50, 180, 197, 91, 176, 239, 83, 16, 99, 19, 240, 127, 12, 172, 38, 203, 116, 173, 65, 47, 200, 163, 234, 80, 135, 208, 140, 115, 18, 85, 226, 182, 169, 126, 245, 84, 72, 168, 253, 78, 209, 147, 59, 136, 23, 220, 142, 218, 247, 185, 98, 13, 52, 206, 198, 150, 102, 79, 105, 187, 237, 186, 119, 36, 90, 107, 162, 109, 137, 30, 122, 194, 216, 223, 128, 195, 251, 21, 111, 190, 67, 224, 121, 120, 143, 157, 73, 177, 199, 141, 95, 213, 202, 46, 134, 61, 215, 89, 156, 178, 123, 37, 64, 110, 14, 183, 145, 193, 189, 60, 167};
		
		Date dateJour = new Date();
		SimpleDateFormat dateAnnee = new SimpleDateFormat("yyyy");
		SimpleDateFormat dateMois = new SimpleDateFormat("MM");
		SimpleDateFormat dateDay = new SimpleDateFormat("dd");
		
		int annee=new Integer(dateAnnee.format(dateJour)).intValue();
		int mois=new Integer(dateMois.format(dateJour)).intValue();
		int jour=new Integer(dateDay.format(dateJour)).intValue();
		
		String idPSformat=idPS;
		if (idPS==null || idPSformat.length()==0) idPSformat="NR";
		
		String chaineACoder=mois+codeEditeur+idPSformat+annee+jour;
		
		String chaineCodee="";
		for (int i=0;i<chaineACoder.length();i++)
		{
			chaineCodee=chaineCodee+(int)(tabConversion[(int)(chaineACoder.substring(i,i+1).charAt(0))]);
		}
		
		String codeSecurite="";
		for (int i=0;i<chaineCodee.length();i++)
		{
			if ((i+1)%3==0) codeSecurite+=chaineCodee.charAt(i);
		}

		System.out.println("CODE SECURITE : "+codeSecurite);
		return codeSecurite;
	}
}
